﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Jerkymon.Web
{
    public class JavaScriptBuilder
    {
        public JavaScriptBuilder()
        {
            this.sb = new StringBuilder();
            this.currIndent = 0;
            this.openBlocks = 0;
            this.format = false;

        }

        public JavaScriptBuilder(bool Formatted)
        {
            this.sb = new StringBuilder();
            this.currIndent = 0;
            this.openBlocks = 0;
            this.format = false;
            this.format = Formatted;
        }

        public void AddCommentLine(params string[] CommentText)
        {
            if (this.format)
            {
                for (int num1 = 0; num1 < this.currIndent; num1++)
                {
                    this.sb.Append("\t");
                }
                this.sb.Append("// ");
                foreach (string text1 in CommentText)
                {
                    this.sb.Append(text1);
                }
                this.sb.Append(Environment.NewLine);
            }
        }


        public void AddLine(params string[] parts)
        {
            if (this.format)
            {
                for (int num1 = 0; num1 < this.currIndent; num1++)
                {
                    this.sb.Append("\t");
                }
            }
            foreach (string text1 in parts)
            {
                this.sb.Append(text1);
            }
            if (this.format)
            {
                this.sb.Append(Environment.NewLine);
            }
            else if (parts.Length > 0)
            {
                this.sb.Append(" ");
            }
        }


        public void CloseBlock()
        {
            if (this.openBlocks < 1)
            {
                throw new InvalidOperationException("JavaScriptBuilder.CloseBlock() called when no blocks open");
            }
            this.currIndent--;
            this.openBlocks--;
            this.AddLine(new string[] { "}" });
        }


        public void OpenBlock()
        {
            this.AddLine(new string[] { "{" });
            this.currIndent++;
            this.openBlocks++;
        }


        public override string ToString()
        {
            if (this.openBlocks > 0)
            {
                throw new InvalidOperationException("JavaScriptBuilder: code blocks are still open");
            }
            return string.Format("<script language=\"javascript\">{0}<!--{0}{1}// -->{0}</script>", Environment.NewLine, this.sb);
        }


        public int Indent
        {
            get
            {
                return this.currIndent;
            }
            set
            {
                this.currIndent = value;
            }
        }

        // Fields
        private int currIndent;
        private bool format;
        private int openBlocks;
        private StringBuilder sb;
    }
}
